<template>
  <a-card :bordered="false" class="card-area" height="100%" bottom="auto">
    <a-form :form="form" :id="id">
      <a-form-item label='公司名称' v-bind="formItemLayout">
        <a-input style="width: 100%" v-decorator="['company']"/>
      </a-form-item>
      <a-form-item label='发件人' v-bind="formItemLayout">
        <a-input v-decorator="['sendName',{rules: [{ required: true, message: '发货人姓名不能为空'}]}]" style="width: 100%" placeholder="请输入发货人姓名"/>
      </a-form-item>
      <a-form-item label='手机号码' v-bind="formItemLayout">
        <a-input v-decorator="['mobile', {rules: [{required: true, pattern: '^0?(13[0-9]|15[012356789]|17[013678]|18[0-9]|14[57])[0-9]{8}$', message: '请输入正确的手机号'}]}]" style="width: 100%" placeholder="请输入发货人手机号码"/>
      </a-form-item>
      <!-- 发货地址使用三级联动-->
      <div class="ant-row ant-form-item">
        <div class="ant-col-6 ant-form-item-label">
          <label title="发货地址" class="ant-form-item-required">发货地址</label>
        </div>
        <v-distpicker class="distpickerClass" style="margin-left: 15em" :province="provinceName" :city="cityName" :area="expAreaName" @selected="selected" v-show="addInp"/>
      </div>
      <a-form-item label='详细地址' v-bind="formItemLayout">
        <a-input v-decorator="['address',{rules: [{ required: true, message: '详细地址不能为空'}]}]" style="width: 100%" placeholder="请输入发货详细地址"/>
      </a-form-item>
      <a-form-item label='邮政编码' v-bind="formItemLayout">
        <a-input v-decorator="['postCode',{rules: [
        { required: true, message: '邮政编码不能为空'},
        {pattern:'^[1-9]\\d{5}(?!\\d)$',message:'邮政编码必须为6位数字'}
        ]}]" style="width: 100%" placeholder="请输入发货所在地邮政编码"/>
      </a-form-item>
    </a-form>
    <div class="bootom-button" style="float:right;">
      <a-popconfirm title="确定放弃编辑？" @confirm="onClose" okText="确定" cancelText="取消">
        <a-button style="margin-right: .8rem">取消</a-button>
      </a-popconfirm>
      <a-button @click="handleSubmit" type="primary" :loading="loading">提交</a-button>
    </div>
  </a-card>
</template>

<script>
const formItemLayout = {
  labelCol: {span: 6},
  wrapperCol: {span: 18}
}
export default {
  name: 'OrderSenderEdit',
  data () {
    return {
      loading: false,
      formItemLayout,
      addInp: true,
      form: this.$form.createForm(this),
      provinceName: '',
      cityName: '',
      expAreaName: '',
      id: ''
    }
  },
  created () {
    const bind = arr => data => arr.reduce((r1, r2) => {
      r1[r2] = this.$form.createFormField({
        value: data[r2]
      })
      return r1
    }, {})
    // 获取接口OrderSender对象和等等的所有数据
    this.$api.freight.getOrderSenderInfo(this.$route.query.id).then((r) => { // 接收相同id的那个mcb对象
      console.log(r)
      let data = r.data.data
      this.provinceName = data.provinceName
      this.cityName = data.cityName
      this.expAreaName = data.expAreaName
      this.id = data.id
      this.form = this.$form.createForm(this, {
        mapPropsToFields: () => {
          return bind(['company', 'username', 'sendName', 'mobile', 'address', 'postCode'])(data)
        }
      })
      console.log(data)
    })
  },
  methods: {
    selected (data) {
      this.provinceName = data.province.value
      this.cityName = data.city.value
      this.expAreaName = data.area.value
    },
    // 清空
    reset () {
      this.provinceName = ''
      this.cityName = ''
      this.expAreaName = ''
      this.form.resetFields()
      this.loading = false
    },
    onClose () {
      this.reset()
      this.$emit('close')
    },
    // 修改提交
    handleSubmit () {
      if (this.provinceName === undefined || this.expAreaName === '区') {
        this.$message.warning('请选择所在地区')
        return
      }
      this.form.validateFields((err, values) => {
        console.log(values)
        values.provinceName = this.provinceName
        values.cityName = this.cityName
        values.expAreaName = this.expAreaName
        values.id = this.id
        if (!err) {
          this.loading = true
          console.log(values)
          this.$api.freight.putOrderSenderInfo(values).then(() => {
            this.$emit('success')
            this.$message.success('修改发件信息成功')
            this.reset()
            this.$router.push({path: `sender`})
          }).catch(() => {
            this.loading = false
          })
        }
      })
    }
  }
}
</script>

<style scoped>
  .distpickerClass {
    line-height: 40px;
  }
  .distpicker-address-wrapper>>>select {
    display: inline-block;
    padding: 4px 11px;
    height: 32px;
    font-size: 14px;
    line-height: 1.5;
  }
</style>
